/*
 * syscall.c
 */

/*
   Copyright 2007 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#define SYSCALL_MESSAGE					4
#define SYSCALL_MESSAGE_SEND_PULSE		0
#define SYSCALL_MESSAGE_RECEIVE_PULSE	1
#define SYSCALL_MESSAGE_SEND			2
#define SYSCALL_MESSAGE_RECEIVE			3
#define SYSCALL_MESSAGE_REPLY			4
#define SYSCALL_MESSAGE_PIPE_CREATE		5
#define SYSCALL_MESSAGE_PIPE_DESTROY	6

#define SYSCALL_THREAD 					2
#define SYSCALL_THREAD_SLEEP			7
#define SYSCALL_THREAD_WAIT				5
#define SYSCALL_THREAD_GET_TID			9

#asm

.define _message_createPipe
_message_createPipe:
	push bp
	mov bp,sp
	
	mov eax,*SYSCALL_MESSAGE_PIPE_CREATE
	push eax
	mov eax,*SYSCALL_MESSAGE
	push eax
	
	int $30
	
	! move result in eax into dx:ax. now performed by the vm86 monitor
	! mov edx,eax
	! shr edx,*16
	
	add sp,*8
	
	pop bp
	ret

.define _message_destroyPipe
_message_destroyPipe:
	push bp
	mov bp,sp
	
	mov eax,[bp+4]
	push eax
	mov eax,*SYSCALL_MESSAGE_PIPE_DESTROY
	push eax
	mov eax,*SYSCALL_MESSAGE
	push eax
	
	int $30
	
	add sp,*12
	
	pop bp
	ret
	
#if 1
.define _message_send
_message_send:
	push bp
	mov bp,sp
	mov eax,dword [bp+24]
	push eax
	mov eax,dword [bp+20]
	push eax
	mov eax,dword [bp+16]
	push eax
	mov eax,dword [bp+12]
	push eax
	mov eax,dword [bp+8]
	push eax
	mov eax,dword [bp+4]
	push eax
	mov eax,*SYSCALL_MESSAGE_SEND
	push eax
	mov eax,*SYSCALL_MESSAGE
	push eax
	int $30
	add sp,*32
	pop bp
	ret

.define _message_receive
_message_receive:
	push bp
	mov bp,sp
	mov eax,dword [bp+12]
	push eax
	mov eax,dword [bp+8]
	push eax
	mov eax,dword [bp+4]
	push eax
	mov eax,*SYSCALL_MESSAGE_RECEIVE
	push eax
	mov eax,*SYSCALL_MESSAGE
	push eax
	int $30
	add sp,*20
	pop bp
	ret

.define _message_reply
_message_reply:
	push bp
	mov bp,sp
	mov eax,dword [bp+12]
	push eax
	mov eax,dword [bp+8]
	push eax
	mov eax,dword [bp+4]
	push eax
	mov eax,*SYSCALL_MESSAGE_REPLY
	push eax
	mov eax,*SYSCALL_MESSAGE
	push eax
	int $30
	add sp,*20
	pop bp
	ret

.define _thread_sleep
_thread_sleep:
	push bp
	mov bp,sp
	mov eax,dword [bp+4]
	push eax
	mov eax,*SYSCALL_THREAD_SLEEP
	push eax
	mov eax,*SYSCALL_THREAD
	push eax
	int $30
	add sp,*12
	pop bp
	ret
	
.define _thread_wait
_thread_wait:
	push bp
	mov bp,sp
	mov eax,*SYSCALL_THREAD_WAIT
	push eax
	mov eax,*SYSCALL_THREAD
	push eax
	int $30
	add sp,*8
	pop bp
	ret	
#endif

#endasm
